#!/bin/bash

#clean the data file
echo \#start >figure12_k_1.dat
echo \#start >figure12_k_2.dat
echo \#start >figure12_k_3.dat

#run the simulation 10 times
#simulation_red minthresh
simulation_red()
{ # $1 is the first parameter of simulation()
n1=$1

for ((simulation_i=1; simulation_i<=10; simulation_i++ )) do
	#red.tcl minthresh seed 
	ns red.tcl $n1 $simulation_i
done
}

#
set_k_1_red()
{
	cp red_k_1.cc /home/harvey/ns-allinone-2.34/ns-2.34/queue/red.cc
	cd /home/harvey/ns-allinone-2.34/ns-2.34
	make >temp_file
	cd /home/harvey/Desktop/cs5229_networking_assignment2/google_code/figure12
}
set_k_2_red()
{
	cp red_k_2.cc /home/harvey/ns-allinone-2.34/ns-2.34/queue/red.cc
	cd /home/harvey/ns-allinone-2.34/ns-2.34
	make >temp_file
	cd /home/harvey/Desktop/cs5229_networking_assignment2/google_code/figure12
}
set_k_3_red()
{
	cp red_k_3.cc /home/harvey/ns-allinone-2.34/ns-2.34/queue/red.cc
	cd /home/harvey/ns-allinone-2.34/ns-2.34
	make >temp_file
	cd /home/harvey/Desktop/cs5229_networking_assignment2/google_code/figure12
}


echo 'set the k_3 red in ns'
set_k_3_red
echo 'start the simulation'
#run the simulation with differen buffer size
#for buffersize_i in 3 5 7 9 11 13 15 20 25 30 40 50; do
for ((minthresh_i=1; minthresh_i<=50; minthresh_i=minthresh_i+1 )) do
	#simulation_red minthresh
	simulation_red $minthresh_i >temp_data
	cat temp_data | awk 'BEGIN{delay=0; throughput=0} {if ($2=="delay"){delay=delay+$5}; 
 			if ($2=="throughput"){throughput=throughput+$5}} 
			END{print (delay/10), (throughput/10)}' >> figure12_k_3.dat
done

echo 'set the k_2 red in ns'
set_k_2_red

echo 'start the simulation'
#run the simulation with differen buffer size
#for buffersize_i in 3 5 7 9 11 13 15 20 25 30 40 50; do
for ((minthresh_i=1; minthresh_i<=50; minthresh_i=minthresh_i+1 )) do
	#simulation_red minthresh
	simulation_red $minthresh_i >temp_data
	cat temp_data | awk 'BEGIN{delay=0; throughput=0} {if ($2=="delay"){delay=delay+$5}; 
 			if ($2=="throughput"){throughput=throughput+$5}} 
			END{print (delay/10), (throughput/10)}' >> figure12_k_2.dat
done

echo 'set the k_2 red in ns'
set_k_1_red
echo 'start the simulation'
#run the simulation with differen buffer size
#for buffersize_i in 3 5 7 9 11 13 15 20 25 30 40 50; do
for ((minthresh_i=1; minthresh_i<=50; minthresh_i=minthresh_i+1 )) do
	#simulation_red minthresh
	simulation_red $minthresh_i >temp_data
	cat temp_data | awk 'BEGIN{delay=0; throughput=0} {if ($2=="delay"){delay=delay+$5}; 
 			if ($2=="throughput"){throughput=throughput+$5}} 
			END{print (delay/10), (throughput/10)}' >> figure12_k_1.dat
done

#plot the graph
gnuplot figure12.plt
